phonggap微信分享
package com.jiusem.cordova.wechat; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.net.URL; import com.tencent.mm.sdk.openapi.BaseReq; import com.tencent.mm.sdk.openapi.BaseResp; import com.tencent.mm.sdk.openapi.ConstantsAPI; import com.tencent.mm.sdk.openapi.ShowMessageFromWX; import com.tencent.mm.sdk.openapi.IWXAPI; import com.tencent.mm.sdk.openapi.IWXAPIEventHandler; import com.tencent.mm.sdk.openapi.WXAPIFactory; import com.tencent.mm.sdk.openapi.WXAppExtendObject; import com.tencent.mm.sdk.openapi.SendMessageToWX; import com.tencent.mm.sdk.openapi.WXEmojiObject; import com.tencent.mm.sdk.openapi.WXImageObject; import com.tencent.mm.sdk.openapi.WXMediaMessage; import com.tencent.mm.sdk.openapi.WXMusicObject; import com.tencent.mm.sdk.openapi.WXTextObject; import com.tencent.mm.sdk.openapi.WXVideoObject; import com.tencent.mm.sdk.openapi.WXWebpageObject; import com.tencent.mm.sdk.openapi.SendAuth; import com.tencent.mm.sdk.openapi.WXFileObject; import com.jiusem.cordova.wechat.wechatshare.Util; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaWebView; import org.apache.cordova.LOG; import org.apache.cordova.PluginResult; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.content.DialogInterface; import android.content.ContextWrapper; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Toast; public class WechatShare extends CordovaPlugin { private static final String SDCARD_ROOT = Environment.getExternalStorageDirectory().getAbsolutePath(); private static WechatShare instance; //修改为你自己的appID public static String appID = "wxd97ca7d7a2fae4dd"; public WechatShare(){ instance = this; } @Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { boolean result = false; try { if (action.equals("register")) { String appId = args.getString(0); this.register(); callbackContext.success(); result = true; } else if (action.equals("unregister")) { this.unregister(); callbackContext.success(); result = true; } else if (action.equals("openWeixin")) { this.open(); callbackContext.success(); result = true; }else if(action.equals("showToast")){ String txt = args.getString(0); this.showToast(txt); } else if (action.equals("send")) { this.register(); JSONObject cfg = args.getJSONObject(0); if (cfg.getString("type").equals("text")) { this.sendText(cfg.getString("text"), cfg.getBoolean("isSendToTimeline")); } else if (cfg.getString("type").equals("image")) { this.sendImage(cfg.getString("data"), cfg.getString("imageType"), cfg.getBoolean("isSendToTimeline")); } else if (cfg.getString("type").equals("music")) { this.sendMusic(cfg.getString("url"), cfg.getBoolean("isLowBand"), cfg.getString("title"), cfg.getString("desc"), cfg.getString("imgUrl"), cfg.getBoolean("isSendToTimeline")); } else if (cfg.getString("type").equals("video")) { this.sendVideo(cfg.getString("url"), cfg.getBoolean("isLowBand"), cfg.getString("title"), cfg.getString("desc"), cfg.getString("imgUrl"), cfg.getBoolean("isSendToTimeline")); } else if (cfg.getString("type").equals("webpage")) { this.sendWebPage(cfg.getString("url"), cfg.getString("title"), cfg.getString("desc"), cfg.getString("imgUrl"), cfg.getBoolean("isSendToTimeline")); } else if (cfg.getString("type").equals("file")) { this.sendFile(cfg.getString("path"), cfg.getString("title"), cfg.getString("desc"), cfg.getString("imgUrl"), cfg.getBoolean("isSendToTimeline")); } callbackContext.success(); result = true; } else { result = false; } } catch (JSONException e) { callbackContext.error("JSON Exception"); result = false; } return result; } public void showToast(String txt){ Context Activity = this.cordova.getActivity().getApplicationContext(); Toast.makeText(Activity, txt, Toast.LENGTH_LONG).show(); } //回调 public static void sendCallBack(int result){ if (instance == null) { return; } instance.webView.sendJavascript("navigator.WechatShare.sendCallBack(" String.valueOf(result) ")"); } private IWXAPI api; public void register() { Context Activity = this.cordova.getActivity().getApplicationContext(); api = WXAPIFactory.createWXAPI(Activity, instance.appID, false); api.registerApp(instance.appID); } public void open() { api.openWXApp(); } //send text public void sendText(String text, boolean isSendToTimeline) { try{ WXTextObject textObj = new WXTextObject(); textObj.text = text; WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = textObj; msg.description = text; SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = String.valueOf(System.currentTimeMillis());//buildTransaction("text"); req.message = msg; int wxSdkVersion = api.getWXAppSupportAPI(); if (wxSdkVersion >= 0x21020001) { req.scene = isSendToTimeline ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession; } else { req.scene = SendMessageToWX.Req.WXSceneSession; } api.sendReq(req); } catch(Exception e) { e.printStackTrace(); } } //send image public void sendImage(String data, String imageType, boolean isSendToTimeline) { File file = null; String path = ""; Bitmap bmp = null; try{ WXImageObject imgObj = new WXImageObject(); if (imageType.equals("url")) { imgObj.imageUrl = data; bmp = BitmapFactory.decodeStream(new URL(data).openStream()); } else if (imageType.equals("path")) { LOG.d("WeChat Plugin", "file path: " SDCARD_ROOT data); if(data.startsWith(SDCARD_ROOT)){ path = data; }else{ path = (SDCARD_ROOT data).replaceAll(" ", "%20"); } file = new File(path); if (!file.exists()) { LOG.d("WeChat Plugin", "file not exists"); return; } else { LOG.d("WeChat Plugin", "get file @" path); } imgObj.setImagePath(path); FileInputStream fis = new FileInputStream(path); bmp = BitmapFactory.decodeStream(fis); } WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = imgObj; Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true); bmp.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBmp, true); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = String.valueOf(System.currentTimeMillis()); req.message = msg; int wxSdkVersion = api.getWXAppSupportAPI(); if (wxSdkVersion >= 0x21020001) { req.scene = isSendToTimeline ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession; } else { req.scene = SendMessageToWX.Req.WXSceneSession; } api.sendReq(req); } catch(Exception e) { e.printStackTrace(); } } //send music public void sendMusic(String url, boolean isLowBand, String title, String desc, String imgUrl, boolean isSendToTimeline) { WXMusicObject music = new WXMusicObject(); if (isLowBand == false) { music.musicUrl = url; } else { music.musicLowBandUrl = url; } WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = music; msg.title = title; msg.description = desc; Bitmap bmp = null; try{ /* if (imgUrl.equals("")) { Context Activity = this.cordova.getActivity().getApplicationContext(); Bitmap thumb = BitmapFactory.decodeResource(Activity.getResources(), R.drawable.music); msg.thumbData = Util.bmpToByteArray(thumb, true); } else { bmp = BitmapFactory.decodeStream(new URL(imgUrl).openStream()); Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true); bmp.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBmp, true); }**/ bmp = BitmapFactory.decodeStream(new URL(imgUrl).openStream()); Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true); bmp.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBmp, true); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("music"); req.message = msg; int wxSdkVersion = api.getWXAppSupportAPI(); if (wxSdkVersion >= 0x21020001) { req.scene = isSendToTimeline ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession; } else { req.scene = SendMessageToWX.Req.WXSceneSession; } api.sendReq(req); } catch(Exception e) { e.printStackTrace(); } } //send video public void sendVideo(String url, boolean isLowBand, String title, String desc, String imgUrl, boolean isSendToTimeline) { WXVideoObject video = new WXVideoObject(); if (isLowBand == false) { video.videoUrl = url; } else { video.videoLowBandUrl = url; } WXMediaMessage msg = new WXMediaMessage(video); msg.title = title; msg.description = desc; Bitmap bmp = null; try{ /* if (imgUrl.equals("")) { Context Activity = this.cordova.getActivity().getApplicationContext(); Bitmap thumb = BitmapFactory.decodeResource(Activity.getResources(), R.drawable.video); msg.thumbData = Util.bmpToByteArray(thumb, true); } else { bmp = BitmapFactory.decodeStream(new URL(imgUrl).openStream()); Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true); bmp.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBmp, true); }*/ bmp = BitmapFactory.decodeStream(new URL(imgUrl).openStream()); Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true); bmp.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBmp, true); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("video"); req.message = msg; int wxSdkVersion = api.getWXAppSupportAPI(); if (wxSdkVersion >= 0x21020001) { req.scene = isSendToTimeline ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession; } else { req.scene = SendMessageToWX.Req.WXSceneSession; } api.sendReq(req); //LOG.d("!!!!!!!!!!!!!!!!!!!!", video.videoUrl); } catch(Exception e) { e.printStackTrace(); } } //send webpage public void sendWebPage(String url, String title, String desc, String imgUrl, boolean isSendToTimeline) { WXWebpageObject webpage = new WXWebpageObject(); webpage.webpageUrl = url; WXMediaMessage msg = new WXMediaMessage(webpage); msg.title = title; msg.description = desc; Bitmap bmp = null; try{ /* if (imgUrl.equals("")) { Context Activity = this.cordova.getActivity().getApplicationContext(); Bitmap thumb = BitmapFactory.decodeResource(Activity.getResources(), R.drawable.webpage); msg.thumbData = Util.bmpToByteArray(thumb, true); } else { bmp = BitmapFactory.decodeStream(new URL(imgUrl).openStream()); Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true); bmp.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBmp, true); }*/ bmp = BitmapFactory.decodeStream(new URL(imgUrl).openStream()); Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true); bmp.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBmp, true); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("webpage"); req.message = msg; int wxSdkVersion = api.getWXAppSupportAPI(); if (wxSdkVersion >= 0x21020001) { req.scene = isSendToTimeline ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession; } else { req.scene = SendMessageToWX.Req.WXSceneSession; } api.sendReq(req); } catch(Exception e) { e.printStackTrace(); } } //send webpage public void sendFile(String path, String title, String desc, String imgUrl, boolean isSendToTimeline) { WXFileObject appdata = new WXFileObject(); if(path.startsWith(SDCARD_ROOT)){ appdata.filePath = path; }else{ appdata.filePath = SDCARD_ROOT path; } WXMediaMessage msg = new WXMediaMessage(appdata); Bitmap bmp = null; try{ /* if (imgUrl.equals("")) { Context Activity = this.cordova.getActivity().getApplicationContext(); Bitmap thumb = BitmapFactory.decodeResource(Activity.getResources(), R.drawable.file); msg.thumbData = Util.bmpToByteArray(thumb, true); } else { bmp = BitmapFactory.decodeStream(new URL(imgUrl).openStream()); Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true); bmp.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBmp, true); }*/ bmp = BitmapFactory.decodeStream(new URL(imgUrl).openStream()); Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true); bmp.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBmp, true); msg.title = title; msg.description = desc; msg.mediaObject = appdata; SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("appdata"); req.message = msg; int wxSdkVersion = api.getWXAppSupportAPI(); if (wxSdkVersion >= 0x21020001) { req.scene = isSendToTimeline ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession; } else { req.scene = SendMessageToWX.Req.WXSceneSession; } api.sendReq(req); } catch(Exception e) { e.printStackTrace(); } } //unregister public void unregister() { api.unregisterApp(); } private String buildTransaction(final String type) { return (type == null) ? String.valueOf(System.currentTimeMillis()) : type System.currentTimeMillis(); } }
评论